# encoding: utf-8

class CommentApi < Grape::API
  helpers ::ToolKit
  prefix :articles

  resource '/:id/comments' do
    after_validation do
      @article = Article.where(id: params[:id]).first
    end
    get do
      error!({message: '不存在此博文！'}.as_json, 404) unless @article
      data, count= @article.paginate_comments params[:start], params[:end]
      paginate_result data, count
    end

    post do
      Comment.post(@article, params[:content], @current_user)
    end

    resource ':comment' do
      after_validation do
        @comment = Comment.find_by(id: params[:comment_id])
      end

      desc '删除该评论'
      delete do
        require_authorized!
        @comment.destroy!
      end
    end
  end
end